import re
import xlwt
from openpyxl import Workbook
from numpy import *
import pymysql
import time,datetime

# 获取时间
from heiben_py.main.util_db import DataBaseHandle

ticks = time.time()
# 加载数据库
DbHandle = DataBaseHandle('116.62.51.1', 'root', '123456', 'test', 3306)
# 读取参数文件，将文件根据每行的空格隔开

h_id_s = DbHandle.selectDb_2("SELECT id from machine_hardware_information where hardware_ip='10.88.66.43' limit 1 ")
id = h_id_s[0]['id']
t_id_s = DbHandle.selectDb_2('select task_id from task where h_id = 1  and task_id =1')
task_id = t_id_s[0]['task_id']

with open('io_usage.log','r',encoding='utf8') as f:
    cont = True
    li = []
    while cont:
        cont = f.readline()
        li.append(cont)
        if cont =='\n':
            if('AM' in li[0] or 'PM' in li[0]):
                for i in range(2,len(li)):
                    item = li[i].split()
                    if len(item) == 6:
                        sql = "INSERT INTO iostat (h_id,t_id,date,Device,tps,MB_readsped,MB_wrtnspeedf,MB_read,MB_wrtn)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" % (
                            id,
                            task_id,
                            "'" + li[0].split()[1] + "'",
                            "'" + item[0] + "'",
                            "'" + item[1] + "'",
                            "'" + item[2] + "'",
                            "'" + item[3] + "'",
                            "'" + item[4] + "'",
                            "'" + item[5].replace('\n','') + "'",
                        )
                        DbHandle.insertDB(sql)
            li = []
DbHandle.closeDb()
f.close()